import Vue from "vue";
import VueRouter from "vue-router";

// import Layout from '@/views/layout'
import Login from "@/views/login";
import Home from "@/views/home";
import store from "@/store";
import Welcome from "@/views/welcome";
import User from "@/views/user";
import Rights from "@/views/rights";
import Roles from "@/views/roles";
import Cate from "@/views/cate";
import Params from "@/views/params";
import Orders from "@/views/orders";
import GoodList from "@/views/goodslist";
import AddGoods from "@/views/addgoods";

Vue.use(VueRouter);

const router = new VueRouter({
  routes: [
    { path: "/", redirect: "/home" },
    { path: "/login", component: Login },
    {
      path: "/home",
      component: Home,
      redirect: "/welcome",
      children: [
        {
          path: "/welcome",
          component: Welcome,
        },
        {
          path: "/users",
          component: User,
        },
        {
          path: "/rights",
          component: Rights,
        },
        {
          path: "/roles",
          component: Roles,
        },
        {
          path: "/categories",
          component: Cate,
        },
        {
          path: "/params",
          component: Params,
        },
        {
          path: "/orders",
          component: Orders,
        },
        {
          path: "/goods",
          component: GoodList,
        },
        {
          path: "/goods/add",
          component: AddGoods,
        },
      ],
    },
  ],
});

router.beforeEach((to, from, next) => {
  const { token } = store.state.user;
  if (to.path !== "/login" && !token) return next("/login");
  next();
});

export default router;
